package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GradeBean;
import com.project.bean.RecordBean;
import com.project.bean.UserBean;
import com.project.mapper.IRecordMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IRecordMapper recordMapper;

    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", userName);

        UserBean user = userMapper.selectOne(qw);
        Md5Hash md5Hash = new Md5Hash(pwd, user.getSalt(), 1024);
        //判断密码
        if (user.getPwd().equals(md5Hash.toHex())) {
            //添加登录日志信息
            String name = user.getName();
            Calendar c = Calendar.getInstance();
            RecordBean record = new RecordBean("用户" + name + "在" +
                    c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + "登录了系统");
            recordMapper.insert(record);
            //查询用户的权限集合
            List<GradeBean> list = userMapper.findGradeList(user.getId());
            user.setGradeBeanList(list);
            return user;
        } else {
            return null;
        }

    }

    @Override
    public void updatePwd(Integer id, String newPwd) {
        UserBean user = userMapper.selectById(id);//通过ID找到用户对象
        user.setSalt(this.getSalt());
        //修改密码并进行加密
        Md5Hash md5Hash = new Md5Hash(newPwd, user.getSalt(), 1024);
        user.setPwd(md5Hash.toHex());
        userMapper.updateById(user);//更新数据
    }

    @Override
    public IPage<UserBean> findByItem(Integer pageNO, String name) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("u_userName", name);
        }
        return userMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public boolean findByName(String name) {//通 过用户名查询用户是否存在,存在返回true,不存在返回false
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", name);
        UserBean user = userMapper.selectOne(qw);
        if (user != null) {
            return true;
        }
        return false;
    }

    @Override
    public void add(UserBean userBean, int[] role) {
        userMapper.insert(userBean);//添加用户
        userMapper.addRoleArray(userBean.getId(), role);//添加用户权限
    }

    @Override
    public void del(Integer id) {
        userMapper.delUserRole(id);//删除用户拥有的角色信息
        userMapper.deleteById(id);//删除用户
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean user = userMapper.selectById(id);//通过ID查找用户
        user.setRoleBeanList(userMapper.findByRole(id));//查找用户角色集合
        user.setGradeBeanList(userMapper.findGradeList(id));//查找用户权限集合
        userMapper.updateById(user);//更新
        return user;
    }
    @Override
    public UserBean findByNameBean(String name) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName", name);
        UserBean user = userMapper.selectOne(qw);
        user.setRoleBeanList(userMapper.findByRole(user.getId()));//查找用户角色集合
        user.setGradeBeanList(userMapper.findGradeList(user.getId()));//查找用户权限集合
        return user;

//        userMapper.updateById(user);//更新
    }

    @Override
    public void updateInfo(Integer id, int[] arrays) {
        userMapper.delUserRole(id);//删除用户拥有的角色信息
        userMapper.addRoleArray(id, arrays);//添加用户的新权限
    }

    //得到salt
    private String getSalt() {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            int index = (int) (Math.random() * str.length());
            sb.append(str.charAt(index));
        }
        return sb.toString();
    }
}
